package com.cardmanager.pro.pojo.schedule;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.MoreObjects;
import org.inferred.freebuilder.FreeBuilder;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry;


/**
 * 还款计划日程信息。
 *
 * @author Fuchun
 * @since 1.0
 */
@FreeBuilder
public abstract class ScheduleItem implements Serializable {

    public abstract LocalDate getDate();

    public abstract List<ItemPart> getItemParts();

    /** 当日的手续费。*/
    @JsonIgnore
    public abstract BigDecimal getDayCommission();

    /** 当日的交易费。*/
    @JsonIgnore
    public abstract BigDecimal getDayTradingFee();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ScheduleItem that = (ScheduleItem) o;
        return Objects.equals(getDate(), that.getDate()) &&
                Objects.equals(getItemParts(), that.getItemParts());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getDate(), getItemParts());
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("date", getDate())
                .add("items", getItemParts())
                .toString();
    }

    public static class Builder extends ScheduleItem_Builder {

        @Override
        public ScheduleItem build() {
            BigDecimal dc = BigDecimal.ZERO, dtf = BigDecimal.ZERO;
            for (ItemPart itemPart : getItemParts()) {
                dc = dc.add(itemPart.getCommission());
                dtf = dtf.add(itemPart.getTradingFee());
            }
            setDayCommission(dc).setDayTradingFee(dtf);
            return super.build();
        }
    }
}
